package com.free.note.util;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * FTP实例
 */
public class FTPDemo {
    public static void main(String[] args) {
        String server = "localhost";
        String username = "caohonghua";
        String password = "******";
        FTPClient ftpClient = null;
        try {
            ftpClient = getFTPClient(server, username, password);
            System.out.println(ftpClient.getReplyString());
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.err.println("连接FTP服务器失败！");
                System.exit(1);
            }
            //获取当前工作空间
            String cwd = ftpClient.printWorkingDirectory();
            System.out.println(cwd);
            System.out.println("--------------");
            //列出当前路径下的文件及其文件夹
            for (FTPFile ftpFile : ftpClient.listFiles()) {
                System.out.println(ftpFile.toString());
            }
            System.out.println("--------------");
            //上传文件
            String source = "/tmp/test";
            String dest = cwd;
            System.out.println(upload(ftpClient, source, dest));
            //下载文件
            source = "/Users/caohonghua/downloadTest";
            dest = "/tmp/downloadTest";
            System.out.println(download(ftpClient, source, dest));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(ftpClient)) {
                try {
                    ftpClient.logout();
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static boolean download(FTPClient ftpClient, String source, String dest) {
        boolean flag = false;
        try (OutputStream outputStream = Files.newOutputStream(Paths.get(dest))) {
            flag = ftpClient.retrieveFile(source, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean upload(FTPClient ftpClient, String source, String dest) {
        boolean flag = false;
        Path path = Paths.get(source);
        try (InputStream inputStream = Files.newInputStream(path)) {
            ftpClient.changeWorkingDirectory(dest);
            flag = ftpClient.storeFile(path.getFileName().toString(), inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static FTPClient getFTPClient(String server, String username, String password) throws IOException {
        FTPClient ftpClient = new FTPClient();
        FTPClientConfig config = new FTPClientConfig();
        config.setServerTimeZoneId("Asia/Shanghai");
        ftpClient.configure(config);
        ftpClient.connect(server);
        ftpClient.login(username, password);
        return ftpClient;
    }
}
